package com.zj.jdbc3Batis;

import com.zj.bean.UserInfo;
import com.zj.jdbc3Batis.service.JdbcService2;
import com.zj.jdbc3Batis.tools.proxy.ServiceInterceptor;
import net.sf.cglib.proxy.Enhancer;

import java.sql.SQLException;
import java.util.Date;
import java.util.List;

/**
 * Hello world!
 *
 */
public class AppTest
{
    public static void main( String[] args ) throws SQLException {
//        System.setProperty(DebuggingClassWriter.DEBUG_LOCATION_PROPERTY, "D:\\java_workapace");
        testServiceWithProxy();

    }

    public static void testString(){
        String sql="insert into user_inf(name,sex) values(#{user.name},#{user.sex})";
        String pattern= "#\\{([^}])+\\}";
        String result = sql.replaceAll(pattern,"?");
        System.out.println(result);
    }


        public static void  testServiceWithProxy() throws SQLException {
            UserInfo user=new UserInfo();
            user.setName("mike3");
            user.setSex(new Byte("0"));
            user.setPhoneNumber("1654567342");
            user.setAddress("汉中市7号");
            user.setBirthday(new Date());
            //创建Enhancer对象，类似于JDK动态代理的Proxy类，下一步就是设置几个参数
            Enhancer enhancer = new Enhancer();
            //设置目标类的字节码文件
            enhancer.setSuperclass(JdbcService2.class);
            //设置回调函数
            enhancer.setCallback(new ServiceInterceptor());
            //这里的creat方法就是正式创建代理类
            JdbcService2 jdbcService = (JdbcService2)enhancer.create();
            List<UserInfo> infos = jdbcService.doBusiness(user, 1);
            for (UserInfo uu:infos) {
                System.out.println(uu);
            }
        }

    public static boolean isWrapClass(Class clz) {
        if(clz.equals(String.class)||clz.equals(Date.class)){
            return false;
        }
        return !clz.isPrimitive();
    }

}
